package mobilesales;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import com.mobilesales.android.model.products.*;
import com.mobilesales.android.model.tasks.Schedule;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.model.clients.ClientGroup;
import com.mobilesales.android.model.employees.EmployeeAccess;
import com.mobilesales.android.services.*;

import mobilesales.android.client.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ClientActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try {
			/*String endpoint = "http://10.0.2.2:19048/RestServices/ProductServiceREST.svc";
			Configuration conf = new Configuration(endpoint);
			ProductServiceClient client = new ProductServiceClient(conf);
			Product prod = client.getProduct(15);

			Log.i("product", (prod != null) ? prod.toString() : "null");*/
			/*String endpoint = "http://10.0.2.2:19048/RestServices/ClientServiceREST.svc";
			Configuration conf = new Configuration(endpoint);
			ClientServiceClient client = new ClientServiceClient(conf);
			Client cl = client.getClient(1);
			Log.i("client", (cl != null) ? cl.toString() : "null");
			cl.Id = 0;
			client.storeClient(cl);
			List<Client> cls = client.getClientList();
			for(Client cli : cls){
				Log.i("client_list", String.valueOf(cli.Id)+ ", "+cli.Name);
			}
			ClientGroup clientGroup = client.getClientGroup(1);
			Log.i("clientGroup", (clientGroup != null) ? clientGroup.toString() : "null");
			List<ClientGroup> clientGroups = client.getClientGroupList();
			for(ClientGroup clg : clientGroups){
				Log.i("clientgroup_list", String.valueOf(clg.Id) + ", "+clg.Name);
			}*/
			/*
			String endpoint = "http://10.0.2.2:19048/RestServices/EmployeeServiceREST.svc";
			Configuration conf = new Configuration(endpoint);
			EmployeeServiceClient client = new EmployeeServiceClient(conf);
			client.sendMessage(2, 2, "subject", "content");
			*/
			
			/*String endpoint = "http://10.0.2.2:19048/RestServices/TaskServiceREST.svc";
			Configuration conf = new Configuration(endpoint);
			TaskServiceClient client = new TaskServiceClient(conf);
			List<Schedule> scheds = client.getScheduleList(2);
			Schedule sched = scheds.get(0);
			String date = sched.StartTime;
			int id = sched.Id;
			Schedule sched1 = client.getSchedule(id);
			Log.i("schedule", sched1.StartTime);
			Schedule sched2 = client.getScheduleByDate(2, date);
			Log.i("schedulebydate", sched2.StartTime);
			//Schedule sched = client.getSchedule(1);
			 */
			/*String endpoint = "http://10.0.2.2:19048/RestServices/AuthServiceREST.svc";
			Configuration conf = new Configuration(endpoint);
			AuthServiceClient client = new AuthServiceClient(conf);
			client.Login("a", "qwerty", "s");
			*/
			String endpointAuth = "http://10.0.2.2:19048/RestServices/AuthServiceREST.svc";
			Configuration confAuth = new Configuration(endpointAuth);
			AuthServiceClient authClient = new AuthServiceClient(confAuth);
			EmployeeAccess employeeAccess = authClient.Login("uxon", "qwerty", "c2dm");
			
			String endpoint = "http://10.0.2.2:19048/RestServices/EmployeeServiceREST.svc";
			Configuration conf = new Configuration(endpoint, employeeAccess.Token);
			EmployeeServiceClient client = new EmployeeServiceClient(conf);
			client.sendMessage(4, 4, "elo", "eloelo");
			client.DeleteMessage(2);
		} catch (Exception e) {
			Log.e("exc2", e.getMessage(), e);
		}
	}



	public String readFully(Reader reader) throws IOException {
		char[] arr = new char[8 * 1024]; // 8K at a time
		StringBuffer buf = new StringBuffer();
		int numChars;

		while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
			buf.append(arr, 0, numChars);
		}

		return buf.toString();
	}
}